import { getMapLayers } from '@/map'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import parseStyle from '@/map/libs/utils/style'

export const addHighlight = (map, features, style) => {
  const layers = getMapLayers(map).filter(item => {
    return item.values_.name === 'tem'
  })
  let heightLayer = layers[0] || null
  if (heightLayer) {
    heightLayer.setSource(new VectorSource({ features }))
  } else {
    heightLayer = new VectorLayer({
      name: 'tem',
      source: new VectorSource({ features }),
    })
    heightLayer.setZIndex(9999)
  }

  if(style && typeof style === 'function'){
    heightLayer.setStyle(style())
  } else {
    heightLayer.setStyle(parseStyle(style))
  }
  map.addLayer(heightLayer)
}

export const removeHighlight = (map) => {
  const layers = getMapLayers(map).find(item => {
      return item.values_.name === 'tem'
  })
  if (layers) {
    map.removeLayer(layers)
  }
}